import type { AxiosError, AxiosInstance } from "axios"

export function retry(instance: AxiosInstance, error: AxiosError) {
    const config: any = error.config
    const { waitTime, count } = config.retryConfig ?? {}

    config.currentConfig = config.currentConfig ?? 0

    if (config.currentConfig >= count) {
        return Promise.reject(error)
    }
    config.currentConfig++

    console.log(`第${config.currentConfig}次重连`)
    return wait(waitTime).then(() => { instance(config) })
}

function wait(waitTime: number) {
    return new Promise((resolve) => setTimeout(resolve, waitTime))
}